﻿using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LaserAutoTest.Common.RabbitMqs
{
    public class Channels
    {
        private static IModel channel;
        private static readonly object lockObject = new object();
        public static readonly string HostName = ConfigurationManager.AppSettings["RabbitMQIP"];
        public static readonly string Exchange = ConfigurationManager.AppSettings["Exchange"];

        private Channels()
        {
        }

        public static IModel Channel
        {
            get
            {
                if (channel == null)
                {
                    lock (lockObject)
                    {
                        if (channel == null)
                        {
                            var factory = new ConnectionFactory();
                            factory.HostName = HostName;
                            factory.Port = 5672;
                            factory.UserName = "admin";
                            factory.Password = "admin";
                            var connection = factory.CreateConnection();
                            channel = connection.CreateModel();
                            var exchange = Exchange;
                            channel.ExchangeDeclare(exchange, ExchangeType.Fanout);//发布订阅模式
                        }
                    }
                }
                return channel;
            }
        }
    }
}
